package cn.edu.scau.cmi.lvmingyan.hibernate.toClient;


import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.lvmingyan.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.lvmingyan.hibernate.domain.Student;
import cn.edu.scau.cmi.lvmingyan.hibernate.domain.Teacher;


public class StudentClient {
	
	private StudentDAO studentDAO;
	private Session session;
	private Transaction transaction;
	
	public StudentClient() {}
	
	public StudentClient(Session session) {
		this.session = session;
		studentDAO = new StudentDAO(this.session);
	}
	
	public void saveStudent(Student student) {
		transaction = session.beginTransaction();
		studentDAO.save(student);
		System.out.println("添加学生成功！");
		transaction.commit();
	}
	
	public void updateStudentName(Student student) {
		transaction = session.beginTransaction();
		studentDAO.updateName(student);
		System.out.println("修改学生姓名成功！");
		transaction.commit();
	}
	
	public void updateStudentNumber(Student student) {
		transaction = session.beginTransaction();
		studentDAO.updateNumber(student);
		System.out.println("修改学生学号成功！");
		transaction.commit();
	}
	
	public void deleteStudent(Student student) {
		transaction = session.beginTransaction();
		studentDAO.delete(student);
		session.flush();
		session.evict(student);
		System.out.println("删除学生成功！");
		transaction.commit();
	}
	
	public void select(Student student,Teacher tutor,int flag) {
		transaction = session.beginTransaction();
		Student stu = (Student) session.get(Student.class, student.getId());
		Teacher t = (Teacher) session.get(Teacher.class, tutor.getId());
		t.getStudents().add(stu);
		stu.setTutor(t);
		if(flag == 7) {
			System.out.println("学生选择指导教师成功!");
		}else if(flag == 8) {
			System.out.println("学生修改指导教师成功!");
		}
		transaction.commit();
	}
}
